package com.zyh.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zyh.common.annotation.Log;
import com.zyh.common.core.controller.BaseController;
import com.zyh.common.core.domain.AjaxResult;
import com.zyh.common.core.page.TableDataInfo;
import com.zyh.common.enums.BusinessType;
import com.zyh.common.utils.poi.ExcelUtil;
import com.zyh.exam.domain.ExamPaper;
import com.zyh.exam.service.IExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 测评卷管理Controller
 *
 * @author zyh
 * @date 2025-02-20
 */
@RestController
@RequestMapping("/exam/paper")
public class ExamPaperController extends BaseController {
    @Autowired
    private IExamPaperService examPaperService;


    /**
     * 更新测评卷发布状态
     */
    //@PreAuthorize("@ss.hasPermi('exam:paper:published')")
    @Log(title = "测评卷管理", businessType = BusinessType.UPDATE)
    @PutMapping("/publishedPaper")
    public AjaxResult publishedPaper(@RequestBody ExamPaper examPaper) {
        logger.info("publishedPaper入参examPaper-->{}", examPaper);
        ExamPaper updateExamPaper = examPaperService.selectExamPaperById(examPaper.getId());
        // 判断测评卷状态
        if (updateExamPaper.getIsPublished().equals(examPaper.getIsPublished())) {
            // 状态相同
            return AjaxResult.error("测评卷发布状态未改变，请刷新并重试！");
        } else {
            updateExamPaper.setIsPublished(examPaper.getIsPublished());
            return examPaperService.publishedPaper(updateExamPaper);
        }
    }

    /**
     * 点击开始测评按钮时要后端校验一次当前时间是否大于测评卷的开始时间
     *
     * @param id
     * @return
     */
    @GetMapping("/checkStatus/{id}")
    public AjaxResult checkStatus(@PathVariable("id") Long id) {
        ExamPaper examPaper = examPaperService.getOne(
                new LambdaQueryWrapper<ExamPaper>()
                        .eq(ExamPaper::getId, id));
        Date startTime = examPaper.getStartTime();
        if (Objects.nonNull(startTime)) {
            // 获取当前时间
            Date now = new Date();
            // 比较当前时间是否在 startTime 之后
            if (now.after(startTime)) {
                // 将状态改为已结束
                examPaper.setStatus("2");
                examPaperService.updateExamPaper(examPaper);
                return AjaxResult.error("测评卷已结束！，请刷新");
            }
        }
        return AjaxResult.success();
    }


    /**
     * 查询测评卷管理列表
     */
    //@PreAuthorize("@ss.hasPermi('exam:paper:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamPaper examPaper) {
        startPage();
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        return getDataTable(list);
    }

    /**
     * 导出测评卷管理列表
     */
    //@PreAuthorize("@ss.hasPermi('exam:paper:export')")
    @Log(title = "测评卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamPaper examPaper) {
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        ExcelUtil<ExamPaper> util = new ExcelUtil<ExamPaper>(ExamPaper.class);
        util.exportExcel(response, list, "测评卷管理数据");
    }

    /**
     * 获取测评卷详细信息
     */
    //@PreAuthorize("@ss.hasPermi('exam:paper:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        logger.info("getInfo入参id-->{}", id);
        return success(examPaperService.selectExamPaperById(id));
    }

    /**
     * 新增测评卷管理
     */
    //@PreAuthorize("@ss.hasPermi('exam:paper:add')")
    @Log(title = "测评卷管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamPaper examPaper) {
        logger.info("add入参examPaper-->{}", examPaper);
        return toAjax(examPaperService.insertExamPaper(examPaper));
    }

    /**
     * 修改测评卷管理
     */
    //@PreAuthorize("@ss.hasPermi('exam:paper:edit')")
    @Log(title = "测评卷管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamPaper examPaper) {
        logger.info("edit入参examPaper-->{}", examPaper);
        return toAjax(examPaperService.updateExamPaper(examPaper));
    }

    /**
     * 删除测评卷管理
     */
    //@PreAuthorize("@ss.hasPermi('exam:paper:remove')")
    @Log(title = "测评卷管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examPaperService.deleteExamPaperByIds(ids));
    }
}
